﻿@using Sample.Models
@page "/associations"
@inject AssociationContext Context


<h1>Associations</h1>

<h2>One to One</h2>

<BSButton Color="Color.Primary" @onclick="@OnAdd">Add a Person and Address</BSButton>

<BSButton Color="Color.Primary" @onclick="@OnChange">Change Address</BSButton>

<h2>One to Many</h2>

<BSButton Color="Color.Primary" @onclick="@OnAddMulti">Add a Person with multi Addresses</BSButton>

<BSButton Color="Color.Primary" @onclick="@OnLoadPerson">Load Person</BSButton>

<h2>Delete Test (Should throw exception)</h2>

<BSButton Color="Color.Primary" @onclick="@OnDelete">Add a Person with Addresses then delete Address</BSButton>

<h2>Result</h2>

@if (_person != null)
{
    <div>
        @_person.FirstName
    </div>
    <div>Addresses:</div>
    <BSListGroup>
        @foreach (var address in _person.OtherAddresses)
        {
            <BSListGroupItem>
                <BSListGroupItemHeading>@address.Id</BSListGroupItemHeading>
                <BSListGroupItemText>
                    @address.Street
                </BSListGroupItemText>
            </BSListGroupItem>
        }
    </BSListGroup>
}

@functions {
    Person _person;
    protected async override Task OnInitAsync()
    {
        await Context.Initialize();
    }
    void OnAdd(UIMouseEventArgs e)
    {
        var person = new Person { FirstName = "John", LastName = "Smith" };
        var address = new Address { Street = "221 Baker Streeet", City = "This should be a refrence to address since Address exists in the context" };
        person.HomeAddress = address;
        Context.People.Add(person);
        Context.SaveChanges();
        StateHasChanged();
    }

    void OnChange(UIMouseEventArgs e)
    {
        Context.People[0].HomeAddress.Street = "Changed Streeet";
        Context.SaveChanges();
        Console.WriteLine("Person address changed: {0}", Context.People[0].HomeAddress.Street);
        Console.WriteLine("Address entity changed as well: {0}", Context.Addresses[0].Street);
        StateHasChanged();
    }

    void OnAddMulti(UIMouseEventArgs e)
    {
        var person = new Person { FirstName = "Many", LastName = "Test" };
        var address1 = new Address { Street = "Many test 1", City = "Saved as a reference" };
        var address2 = new Address { Street = "Many test 2", City = "Saved as a reference" };
        person.OtherAddresses = new List<Address> { address1, address2 };
        Context.People.Add(person);
        Context.SaveChanges();
        StateHasChanged();
    }

    void OnLoadPerson(UIMouseEventArgs e)
    {
        _person = Context.People[1];
        StateHasChanged();
    }

    void OnDelete(UIMouseEventArgs e)
    {
        var person = new Person { FirstName = "John", LastName = "Smith" };
        var address = new Address { Street = "221 Baker Streeet", City = "This should be a refrence to address since Address exists in the context" };
        person.HomeAddress = address;
        Context.People.Add(person);
        Context.SaveChanges();

        Context.Addresses.RemoveAt(0);
        Context.SaveChanges();
    }
}